package com.anjiplus.template.gaea.business.work.data.service.impl;

import com.anjiplus.template.gaea.business.work.data.Cache;
import com.anjiplus.template.gaea.business.work.data.entity.Data;
import com.anjiplus.template.gaea.business.work.data.service.DataService;
import com.anjiplus.template.gaea.business.work.point.dao.PointDao;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 数据service实现类
 *
 * @author yxm
 */
@Service
public class DataServiceImpl implements DataService {

    private PointDao pointDao;

    public DataServiceImpl(PointDao pointDao) {
        this.pointDao = pointDao;
    }

    /**
     * 查询全部参数点数据
     *
     * @return
     */
    @Override
    public List<Data> listAll() {
        after();
        return Cache.dataList;
    }

    /**
     * 根据站点id查询参数点数据
     *
     * @param id
     * @return
     */
    @Override
    public List<Data> listByStationId(String id) {
        after();
        return Cache.dataList.stream().filter(item -> item.getPoint().getStationId().equals(id)).collect(Collectors.toList());
    }

    /**
     * 根据参数点id列表查询数据列表
     *
     * @param ids
     * @return
     */
    @Override
    public List<Data> listByPointIds(String[] ids) {
        after();
        return Cache.dataList.stream()
                .filter(item -> DataService.isInclude(item.getPoint().getId(), ids))
                .collect(Collectors.toList());
    }

    /**
     * 根据参数点code列表查询某参数点数据
     *
     * @param codes
     * @return
     */
    @Override
    public List<Data> listByPointCodes(String[] codes) {
        after();
        return Cache.dataList.stream()
                .filter(item -> DataService.isInclude(item.getPoint().getCode(), codes))
                .collect(Collectors.toList());
    }

    /**
     * 查询方法前执行
     */
    public void after() {
        Cache.dataList = pointDao.listAll().stream()
                .map(item -> new Data(item, "-"))
                .collect(Collectors.toList());
        Cache.buildData();
    }

}
